import math
from openpyxl import Workbook
#剩余牌计算2.0
def calculate_probability(total_cards, target_cards, draw_num=2):
    if target_cards > total_cards or draw_num > total_cards:
        return 0.0
    non_target = total_cards - target_cards
    if non_target < draw_num:
        return 1.0
    try:
        fail_comb = math.comb(non_target, draw_num)
        total_comb = math.comb(total_cards, draw_num)
    except ValueError:
        return 0.0
    return 1 - (fail_comb / total_comb)

# 创建Excel工作簿并设置标题
wb = Workbook()
ws = wb.active
ws.append(["剩余牌数", "目标牌数1", "目标牌数2", "目标牌数3", "目标牌数4"])

# 填充数据
for total in range(1, 27):
    row = [total]
    for target in [1, 2, 3, 4]:
        prob = calculate_probability(total, target)
        row.append(prob)
    ws.append(row)

# 保存Excel文件
wb.save(r"E:\PYTHON花札牌计算\花札\card_probability.xlsx")

print("计算完成，结果已保存到 card_probability.xlsx")




